home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / TransSkel 3.24 / Source / SkelAlert.c next >
Text File  |  1996-01-17  |  4KB  |  137 lines

  1. /*
  2.  * SkelAlert ()
  3.  *
  4.  * Present an alert, positioned according to the given positioning type
  5.  * using the current positioning ratios.  The trick is to grab the alert
  6.  * template, make sure it stays in memory, and position its window
  7.  * bounding rectangle before the Alert() call is made.
  8.  *
  9.  * SkelAlert() returns whatever value the alert would normally return,
  10.  * except that an error value is returned if the alert cannot be presented.
  11.  */
  12.  
  13. # include    "TransSkel.h"
  14.  
  15.  
  16. /* dialog box had 8 pixel border */
  17.  
  18. # define    dlogBoxBorder    8
  19.  
  20. /*
  21.  * The initial (default) horizontal and vertical positioning ratios
  22.  * are those given by Apple's (current) Human Interface Guidelines.
  23.  */
  24.  
  25. # define    defHorizRatio    FixRatio (1, 2)
  26. # define    defVertRatio    FixRatio (1, 5)
  27.  
  28. static Fixed    horizRatio = -1;
  29. static Fixed    vertRatio = -1;
  30.  
  31.  
  32. /*
  33.  * Get current positioning ratios.  Make sure to initialize them if
  34.  * they haven't been set yet.  This is necessary because horizRatio
  35.  * and vertRatio can't be initialized to the non-constant values
  36.  * defHorizRatio and defVertRatio.
  37.  *
  38.  * Either parameter can be nil if the caller is not interested in
  39.  * the corresponding value.
  40.  */
  41.  
  42. pascal void
  43. SkelGetAlertPosRatios (Fixed *hRatio, Fixed *vRatio)
  44. {
  45.     if (hRatio != (Fixed *) nil)
  46.     {
  47.         if (horizRatio == (Fixed) -1)
  48.             horizRatio = defHorizRatio;
  49.         *hRatio = horizRatio;
  50.     }
  51.     if (vRatio != (Fixed *) nil)
  52.     {
  53.         if (vertRatio == (Fixed) -1)
  54.             vertRatio = defVertRatio;
  55.         *vRatio = vertRatio;
  56.     }
  57. }
  58.  
  59.  
  60. /*
  61.  * Set current positioning ratios
  62.  */
  63.  
  64. pascal void
  65. SkelSetAlertPosRatios (Fixed hRatio, Fixed vRatio)
  66. {
  67.     horizRatio = hRatio;
  68.     vertRatio = vRatio;
  69. }
  70.  
  71.  
  72. /*
  73.  * Position and present an alert.
  74.  *
  75.  * The alert bounding box is saved before postioning it and restored
  76.  * afterward, in case the resource is used outside of this function.
  77.  */
  78.  
  79. pascal short
  80. SkelAlert (short alrtResNum, ModalFilterUPP filter, short positionType)
  81. {
  82. GrafPtr        oldPort;
  83. AlertTHndl    h;
  84. Rect        origRect;
  85. Rect        refRect;
  86. SignedByte    state;
  87. short        result;
  88. Fixed        hRatio, vRatio;
  89.  
  90.     GetPort (&oldPort);
  91.     h = (AlertTHndl) GetResource ('ALRT', alrtResNum);
  92.     if (h == (AlertTHndl) nil)
  93.     {
  94.         SysBeep (1);
  95.         return (ResError ());
  96.     }
  97.     state = HGetState ((Handle) h);
  98.     HNoPurge ((Handle) h);                /* make sure it's around */
  99.     LoadResource ((Handle) h);            /* and stays around */
  100.     MoveHHi ((Handle) h);
  101.     HLock ((Handle) h);
  102.     origRect = (**h).boundsRect;        /* save original position */
  103.     if (positionType != skelPositionNone)
  104.     {
  105.         /*
  106.          * Inset bounding rectangle so position is effectively done using
  107.          * the structure rectangle rather than the content rectangle.
  108.          */
  109.         InsetRect (&(**h).boundsRect, -dlogBoxBorder, -dlogBoxBorder);
  110.  
  111.         SkelGetReferenceRect (&refRect, positionType);
  112.         SkelGetAlertPosRatios (&hRatio, &vRatio);
  113.         SkelPositionRect (&refRect, &(**h).boundsRect, hRatio, vRatio);
  114.         /*
  115.          * If alert is to be positioned on parent window, alert might be
  116.          * partly invisible if parent is moved partly off the desktop.  If
  117.          * so, reposition alert on parent device.
  118.          */
  119.         if (positionType == skelPositionOnParentWindow
  120.                 && !SkelTestRectVisible (&(**h).boundsRect))
  121.         {
  122.             SkelGetReferenceRect (&refRect, skelPositionOnParentDevice);
  123.             SkelPositionRect (&refRect, &(**h).boundsRect, hRatio, vRatio);
  124.         }
  125.  
  126.         InsetRect (&(**h).boundsRect, dlogBoxBorder, dlogBoxBorder);
  127.     }
  128.     InitCursor ();            /* in case it comes up while cursor is different */
  129.     result = Alert (alrtResNum, filter);
  130.     (**h).boundsRect = origRect;        /* restore original position */
  131.     HUnlock ((Handle) h);
  132.     HSetState ((Handle) h, state);        /* get rid of resource */
  133.     ReleaseResource ((Handle) h);
  134.     SetPort (oldPort);
  135.     return (result);
  136. }
  137.